import requests
from common.read_config import ReadConfig
from common.project_path import *


class HttpRequest:
    def __init__(self):
        self.headers = {"X-Lemonban-Media-Type": "lemonban.v2"}
        self.base_url = ReadConfig().read_config(config_path, "MODE", "test_host")

    def send_request(self, method, url, data, token=None):
        self.__deal_token(token)
        url = self.__deal_url(url)

        if method.upper() == "GET":
            resp = requests.request(method, url=url, params=data, headers=self.headers)
        else:
            resp = requests.request(method, url=url, json=data, headers=self.headers)

        return resp

    def __deal_token(self, token=None):
        if token:
            self.headers["Authorization"] = f"Bearer {token}"

    def __deal_url(self, url):
        if url.startswith("http"):
            url = url
        else:
            url = self.base_url + url
        return url


if __name__ == '__main__':
    hr = HttpRequest()
    # url="http://admin-api.tool.octech.top/uc/v1/login"
    url = "/futureloan/member/register"
    req_data = {"mobile_phone": "15128288928", "pwd": "123456788", "reg_name": "xiaolingto"}
    # req_data={"username":"test","password":"testAdmin"}
    method = "post"
    resp = hr.send_request(method, url, req_data)
    print(resp)
    print(resp.json())
